﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.ComponentModel;
using ComApp;
using ZXing;

namespace AndroidApp
{
    public class BarCode2D
    {
        private static BarCode2D instance;
        public static BarCode2D Instance
        {
            get
            {
                return instance ?? (instance = new BarCode2D());
            }
        }


        public Android.Graphics.Bitmap Create(string text, int widthAndHeight)
        {
            var BLACK = 0xff000000;
            //Dictionary<EncodeHintType, object> hints = new Dictionary<EncodeHintType, object>();
            //hints.Add(EncodeHintType.CHARACTER_SET,"utf-8");
            var matrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
            int width = matrix.Width;
            int height = matrix.Height;
            int[] pixels = new int[width * height];
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    if (matrix[x, y])
                    {
                        unchecked
                        {
                            pixels[y * width + x] = (int)BLACK;
                        }
                    }
                }
            }

            var bitmap = Android.Graphics.Bitmap.CreateBitmap(width, height, Android.Graphics.Bitmap.Config.Argb8888);
            bitmap.SetPixels(pixels, 0, width, 0, 0, width, height);
            return bitmap;
        }
    }

}
